
/**
 *
 */
package com.serviceprovider.business.module.taxpayer.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.sp.transaction.model.TransactionDetail;
import com.serviceprovider.business.module.taxpayer.model.TaxPayerInfo;
import com.serviceprovider.business.module.taxpayer.services.ITaxPayerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

//~--- JDK imports ------------------------------------------------------------

import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**
 * @author mrduongnv@gmail.com
 *
 */
@Controller
public class TaxpayerController extends SimpleFormController {
    @Autowired
    private ITaxPayerService taxPayerService;
    @Autowired
    @Qualifier("validator")
    private Validator        validator;

    @RequestMapping(
        value  = "/serviceprovider/taxpayer/new_application.htm",
        method = RequestMethod.GET
    )
    public ModelAndView newApplicaton() {
        ModelAndView mv = new ModelAndView("serviceprovider/taxpayer/new_application_step1");

        mv.addObject("taxPayerInfo", new TaxPayerInfo());

        return mv;
    }

    @RequestMapping(
        value  = "/serviceprovider/taxpayer/new_application2.htm",
        method = RequestMethod.POST
    )
    public ModelAndView newApplicaton2(@ModelAttribute("taxPayerInfo") TaxPayerInfo taxPayer, BindingResult result) {
        Integer                 ssn                   = taxPayer.getTaxPayerSSNITIN();
        Integer                 traceNumber           = taxPayer.getTransactionNum();
        TaxPayerInfo            taxPayerInfo          = null;
        List<TransactionDetail> transactionDetailList = null;
        ModelAndView            mv                    = new ModelAndView("serviceprovider/taxpayer/new_application_step2");

        ValidationUtils.rejectIfEmptyOrWhitespace((Errors) result, "taxPayerSSNITIN", "ssn.required");

        if (result.hasErrors()) {
            mv.setViewName("serviceprovider/taxpayer/new_application_step1");
        } else {
            if (taxPayer.getAwaitingITIN() == null) {
                taxPayerInfo = taxPayerService.getTaxPayerDetails(ssn);
            }

            if (taxPayerInfo == null) {
                transactionDetailList = taxPayerService.getMatchedTransactionDetails(traceNumber, ssn);
                taxPayerInfo          = new TaxPayerInfo();
                taxPayerInfo.setTaxPayerSSNITIN(ssn);
                mv.addObject("transactionDetail", transactionDetailList);
            } else {
                mv.addObject("duplicateApplication", true);
            }

            mv.addObject("viewTaxPayerInfo", taxPayerInfo);
        }

        return mv;
    }

    @RequestMapping(
        value  = "/serviceprovider/taxpayer/traceTransactionNum.htm",
        method = RequestMethod.POST
    )
    public ModelAndView traceTransaction(@ModelAttribute("taxPayerInfo") TaxPayerInfo taxPayer, BindingResult result) {
        ModelAndView            mv                    = new ModelAndView("serviceprovider/taxpayer/new_application_step2");
        Integer                 traceNumber           = taxPayer.getTransactionNum();
        Integer                 ssn                   = taxPayer.getTaxPayerSSNITIN();
        List<TransactionDetail> transactionDetailList = null;
        TaxPayerInfo            taxPayerInfo          = null;

        transactionDetailList = taxPayerService.getMatchedTransactionDetails(traceNumber, null);
        taxPayerInfo          = new TaxPayerInfo();
        taxPayerInfo.setTaxPayerSSNITIN(ssn);
        mv.addObject("transactionDetail", transactionDetailList);

        if ((transactionDetailList != null) && (transactionDetailList.size() == 1)) {
            TransactionDetail transactionDetail = transactionDetailList.get(0);

            taxPayerInfo.setTaxPayerSSNITIN(transactionDetail.getCustomerSSN());
        }

        mv.addObject("viewTaxPayerInfo", taxPayerInfo);

        return mv;
    }

    @RequestMapping(
        value  = "/serviceprovider/taxpayer/saveNewApplication.htm",
        method = RequestMethod.POST
    )
    public ModelAndView saveNewApplication(@ModelAttribute("taxPayerInfo") TaxPayerInfo taxPayer,
            BindingResult result) {
        ModelAndView mv = new ModelAndView("/serviceprovider/taxpayer/awaiting_payments");

        validator.validate(taxPayer, result);
        System.err.println(result.getFieldError());

        if (!result.hasErrors()) {
            taxPayerService.insertTaxPayerInfo(taxPayer);
        } else {
            mv.setViewName("serviceprovider/taxpayer/new_application_step2");
            mv.addObject("viewTaxPayerInfo", taxPayer);
        }

        return mv;
    }

    @RequestMapping(
        value  = "/serviceprovider/taxpayer/awaiting_payments.htm",
        method = RequestMethod.GET
    )
    public ModelAndView getAwaitingPayments(HttpServletRequest request) {
        return new ModelAndView("serviceprovider/taxpayer/awaiting_payments");
    }

    @RequestMapping(
        value  = "/serviceprovider/taxpayer/paid_payments.htm",
        method = RequestMethod.GET
    )
    public ModelAndView getNewTaxPayerStep2(HttpServletRequest request) {
        return new ModelAndView("serviceprovider/taxpayer/paid_payments");
    }
}



